package com.subway.babynote.db;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.subway.babynote.syncdata.ClientDataRecord;
import com.subway.babynote.syncdata.ClientDataRecordBase;
import com.subway.babynote.syncdata.StringUtil;
import com.subway.babynote.syncdata.tableName;

public class query_clientfeed extends cursorbase {
	SQLiteDatabase db = null;
	String sql = "select a.r_time, b.foodname ,a.quantity, b.unit from feeds a join foods b on a.food_id = b.food_id where a.r_date = ? and a.mark = 1 order by a.r_time asc";

	public query_clientfeed(SQLiteDatabase _db) {
		db = _db;
	}

	public ClientDataRecord.list_ClientDataRecord select(int r_date)
	{
		String[] strs = this.getSelectParam(r_date);
		ClientDataRecord.list_ClientDataRecord ret = new ClientDataRecord.list_ClientDataRecord();
		Cursor cs  = null;
		try
		{
			cs = db.rawQuery(this.sql, strs);
			if(cs.getCount() > 0)
			{
				cs.moveToFirst();
				while(!cs.isAfterLast())
				{
					ClientDataRecordBase r = this.read(cs);
					if(r != null)
						ret.list.add(r);
					cs.moveToNext();
				}
				
			}
		}
		catch(Exception ex)
		{
			logging.err("querybase select", ex.getMessage());
			ret = null;
		}
		finally
		{
			this.releaseCursor(cs);
		}
		return ret;
	}
	
	
	protected String[] getSelectParam(int r_date) {
		
		String str = StringUtil.int2str(r_date);
		// TODO Auto-generated method stub
		String[] strs = (str == null? null : new String[]{str} );
		return strs;
	}

	//select a.r_time, b.foodname ,a.quantity, b.unit from feeds a join foods b on a.food_id = b.food_id where a.r_date = ? and a.mark = 1 order by a.r_time asc 
	
	protected ClientDataRecord.clientFeedRecord read(Cursor cs) {
		ClientDataRecord.clientFeedRecord fr = new ClientDataRecord.clientFeedRecord();
		fr.r_time = cs.getInt(0);
		fr.foodname = cs.getString(1);
		fr.quantity = cs.getFloat(2);
		fr.unit = cs.getString(3);
		
		return fr;
	}
}
